class DiscussionBoardAdminController < ApplicationController
  after_filter :post_log
  layout "main"
  
  def index
    #@user = User.find(:first, :conditions => { :login => 'songrit'})
    #if @user!=nil
    #  current_user = @user
    #end
    
    if current_user!=nil && current_user.role!=nil && (current_user.role.include?("A") || current_user.role.include?("T") || current_user.role.include?("D"))
      redirect_to :action => "menu"
    else
      flash[:notice] = "<font color='red'>ท่านไม่มีสิทธิ์เข้าใช้งานเมนูนี้</font>"
    end
  end
  
  def menu
  end
  
  def manage_forum
    #@forums = Forum.find(:all)
    @forums = Forum.paginate :conditions => ["status_flag != ?", "D"], :per_page => 10, :page=>params[:page]
  end
  
  def new_forum
  end
  
  def create_forum
    forum = Forum.new(params[:forum])
    forum.create_on = Time.now
    forum.create_by = current_user.login
    forum.save
    flash[:notice] = 'เพิ่ม Forum เป็นที่เรียบร้อย'
    redirect_to :action => "manage_forum"
  end
  
  def edit_forum
    @forum = Forum.find(params[:id])
  end
  
  def update_forum
    forum = Forum.update(params[:forum][:id], params[:forum])
    forum.update_on = Time.now
    forum.update_by = current_user.login
    forum.save
    flash[:notice] = 'แก้ไข Forum เป็นที่เรียบร้อย'
    redirect_to :action => "manage_forum"
  end

  def delete_forum
    ## Forum.delete(params[:id])
    ## Make to "D" as "Delete"
    Forum.update(params[:id], :status_flag => "D")
    flash[:notice] = 'ลบ Forum ที่ท่านเลือกเป็นที่เรียบร้อยแล้ว'
    redirect_to :action => 'manage_forum'
  end
  
  def manage_topic
    @forums = Forum.find(:all, :conditions => {:status_flag => 'Y'})
  end
  
  def topic_list
    forum = ''
    status = ''
#    begin
#      forum = params[:topic][:forum_id] if params[:topic][:forum_id]!=nil
#    rescue
#      begin
#        forum = params[:forum_id] if [:forum_id]!=nil
#      rescue
#        forum = ''
#      end
#    end

    if params[:topic] && params[:topic][:forum_id]
      forum = params[:topic][:forum_id]
    elsif params[:forum_id]
      forum = params[:forum_id]
    end

#    begin
#      status = params[:topic][:status_flag] if params[:topic][:status_flag]!=nil
#    rescue
#      begin
#        status = params[:status_flag] if params[:status_flag]!=nil
#      rescue
#        status = ''
#      end
#    end

    if params[:topic] && params[:topic][:status_flag]
      status = params[:topic][:status_flag]
    elsif params[:status_flag]
      status = params[:status_flag]
    end

    #DlearnUtility.save_to_log_file('Start '+Time.now.to_s)
    if forum.blank?
      #DlearnUtility.save_to_log_file('forum is blank '+Time.now.to_s)
      if status.blank?
        #DlearnUtility.save_to_log_file('forum is blank : status is blank '+Time.now.to_s)
        @topics = Topic.paginate :conditions => ["status_flag != ?", "D"], :per_page => 10, :page=>params[:page]
      else
        #DlearnUtility.save_to_log_file('forum is blank : status has value '+Time.now.to_s)
        @topics = Topic.paginate :conditions => {:status_flag => status}, :per_page => 10, :page=>params[:page]
      end
    else
      #DlearnUtility.save_to_log_file('forum has value '+Time.now.to_s)
      if status.blank?
        #@topic_pages, @topics = paginate :topics, :conditions => {:forum_id => params[:topic][:forum_id]}, :per_page => 10
        @topics = Topic.paginate :conditions => ["forum_id = ? and status_flag != ?",forum, "D"], :per_page => 10, :page=>params[:page]
      else
        @topics = Topic.paginate :conditions => {:forum_id => forum, :status_flag => status}, :per_page => 10, :page=>params[:page]
      end
    end
    #DlearnUtility.save_to_log_file('End '+Time.now.to_s)
    @forum_id = forum
    @status_flag = status
  end
  
  def new_answer
    @topic = Topic.find(params[:id])
    @forum_id = (params[:forum_id]!=nil)?((params[:forum_id].empty?)?"":params[:forum_id]):""
    @status_flag = (params[:status_flag]!=nil)?((params[:status_flag].empty?)?"":params[:status_flag]):""
  end
  
  def add_answer
    @answer = Answer.new(params[:answer])
    @answer.forum_id = Topic.find(params[:topic][:id]).forum_id
    @answer.topic_id = params[:topic][:id]
    @answer.create_by = current_user.login
    @answer.create_on = Time.now
    @answer.status_flag = 'Y'
    Topic.update(params[:topic][:id], {:status_flag => 'Y'})
    @answer.save
    forum_id = (params[:forum_id]!=nil)?((params[:forum_id].empty?)?"":params[:forum_id]):""
    status_flag = (params[:status_flag]!=nil)?((params[:status_flag].empty?)?"":params[:status_flag]):""
    # TODO - songrit
    #Notifier.deliver_answer_send(DlearnUtility.get_operator_email+","+User.find(:first, :conditions => {:login => Topic.find(params[:topic][:id]).create_by}).email, answer)
    @message = DlearnUtility.get_answer_body
    mail_body = render_to_string :action => "add_answer", :template => "notifier/answer_send", :layout => false
    user_email = ""
    begin
      user_email = User.find(:first, :conditions => {:login => Topic.find(params[:topic][:id]).create_by}).email
    rescue
    end
    Mail.sendmail(DlearnUtility.get_system_email, DlearnUtility.get_operator_email+","+user_email, DlearnUtility.get_answer_subject, mail_body)
    flash[:notice] = 'รับข้อมูลการตอบกระทู้เป็นที่เรียบร้อยแล้ว'
    redirect_to :action => 'topic_list', "topic[forum_id]" => forum_id, "topic[status_flag]" => status_flag
  end
  
  def delete_topic
    ## Delete Answer in Topic
    #Answer.delete_all["topic_id = ?",params[:id]]
    ## Delete Topic
    #Topic.delete(params[:id])
    
    ## Mark to "D" as "Delete"
    Answer.update_all ({:status_flag => 'D'},["topic_id = ?",params[:id]])
    Topic.update(params[:id], :status_flag => "D")
    forum_id = (params[:forum_id]!=nil)?((params[:forum_id].empty?)?"":params[:forum_id]):""
    status_flag = (params[:status_flag]!=nil)?((params[:status_flag].empty?)?"":params[:status_flag]):""
    flash[:notice] = 'ลบกระทู้ที่ท่านเลือกเป็นที่เรียบร้อยแล้ว'
    redirect_to :action => 'topic_list', "topic[forum_id]" => forum_id, "topic[status_flag]" => status_flag
  end
  
  def operate_topic
    topic_list_array = params[:topic][:id]
    if topic_list_array != nil
      topic_list_condition = ""
      topic_list_array.each do |topic_id|
        if topic_list_condition.empty?
          topic_list_condition += topic_id
        else
          topic_list_condition += (","+topic_id)
        end
      end
      if params[:approve]!=nil
        Topic.update_all({:status_flag => 'Y'}, ("id IN("+topic_list_condition+")"))
        flash[:notice] = 'กระทู้ที่ท่านเลือกผ่านการพิจารณาเป็นที่เรียบร้อยแล้ว'
      elsif params[:reject]!=nil
        Topic.update_all({:status_flag => 'F'}, ("id IN("+topic_list_condition+")"))
        flash[:notice] = 'กระทู้ที่ท่านเลือกไม่ผ่านการพิจารณา'
      elsif params[:delete]!=nil
        #Topic.delete_all("id IN("+topic_list_condition+")")
	Answer.update_all ({:status_flag => 'D'}, ("topic_id IN("+topic_list_condition+")"))
	Topic.update_all({:status_flag => 'D'}, ("id IN("+topic_list_condition+")"))
        flash[:notice] = 'ลบกระทู้ที่ท่านเลือกเป็นที่เรียบร้อยแล้ว'
      end
    end
    forum_id = (params[:forum_id]!=nil)?((params[:forum_id].empty?)?"":params[:forum_id]):""
    status_flag = (params[:status_flag]!=nil)?((params[:status_flag].empty?)?"":params[:status_flag]):""
    redirect_to :action => 'topic_list', "topic[forum_id]" => forum_id, "topic[status_flag]" => status_flag
  end
end
